/******************************************************************************************************
Copyright (C) 2010 Mestrelab Research S.L. All rights reserved.

This file is part of the Mnova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/
//! Print the fit regions
function fitRegionsTest()
{
	var spec = new NMRSpectrum( nmr.activeSpectrum() );
	
	var fitList = spec.fitRegions();
	print(fitList);
	for(var i=0; i<fitList.length; i++)
	{
		var peakList = fitList[i].peaks;
		print(fitList[i].name);
		for(var j=0; j<peakList.length;j++)
		{
			print(peakList[j]);
		}
	}

	mainWindow.activeWindow().update();
}

//! Run fitting
function lineFittingTest()
{
//get the current spectrum
	var spec = new NMRSpectrum( nmr.activeSpectrum() );
	if(spec.isValid()==false)
		throw("Invalid Spectrum");

//check the options
	var p = new NMRProcessing(spec.proc);
	print(p.getParameter("Fitting"));
//modify the number of iterations
	p.setParameter("Fitting.maxIt", 200);
//locks the shape by default, set to 0 to unlock all
	p.setParameter("Fitting.DefaultLocks", 8)
	print(p.getParameter("Fitting"));
	spec.proc = p;

//create a region using an spectrum region, the peaks will be detected in the spectrum
	var reg = new FitRegion(5.7, 6.0, spec);
	reg.name="Reg1";
	print(reg);
	print(reg.peaks)

//Fit the regions and add them to the spectrum
	var regionList = []
	regionList.push(spec.fit(reg, true)); //spec.fit(reg,true); will return a new fitted region. Setting the second parameter to false will hide the fit dialog
	print(regionList);
	
	spec.setFitRegions(regionList);
	spec.update();
	mainWindow.activeDocument.update();
}